package com.example.testapp.activity.util;

import android.content.Context;

import java.io.File;
import java.text.DecimalFormat;

/**
 * Created by l on 2016/10/27.
 */

public class FileUtils {

    // 获取图片缓存大小
    public static String getDiskCacheSize(Context context) {
        int  sizeSum = 0;
        File diskfile = Utils.createDefaultCacheDir(context);
        String[] diskfileList = diskfile.list();
        for(int i =0; i < diskfileList.length; i++){
            File fileSize = new File(diskfile + "/" + diskfileList[i]);
            sizeSum = (int) (sizeSum + fileSize.length());
            System.out.println(sizeSum);
        }
        DecimalFormat df = new DecimalFormat(".00");
        double sizeSumKB = sizeSum / 1024;
        if (sizeSumKB < 1){
            return String.valueOf(sizeSum) + "B";
        }else {
            double sizeSumMB = sizeSumKB / 1024;
            if (sizeSumMB < 1){
                return String.valueOf((int)sizeSumMB) + "KB";
            }else{
                return String.valueOf(df.format(sizeSumMB)) + "KB";
            }
        }
    }
}
